package object;

/**
 * @TODO
 * @Author xu
 * 2025/3/10
 */
import java.util.ArrayList;
import java.util.List;

class Student02 {
    private String name;
    private String studentId;

    public Student02(String name, String studentId) {
        this.name = name;
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

    public String getStudentId() {
        return studentId;
    }

    // 重写equals和hashCode方法，以确保同名且同学号的学生被视为相等
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Student student = (Student) obj;
        return name.equals(student.name) && studentId.equals(student.studentId);
    }

    @Override
    public int hashCode() {
        return name.hashCode() + studentId.hashCode();
    }

    @Override
    public String toString() {
        return "Student{name='" + name + "', studentId='" + studentId + "'}";
    }
}

class StudentManagementSystem {
    private List<Student> students;

    public StudentManagementSystem() {
        students = new ArrayList<>();
    }

    // 添加学生的方法
    public boolean addStudent(Student student) {
        if (students.contains(student)) {
            System.out.println("学生已存在，无法添加: " + student);
            return false; // 不允许添加
        }
        students.add(student);
        System.out.println("成功添加学生: " + student);
        return true; // 添加成功
    }

    // 打印所有学生信息
    public void printAllStudents() {
        System.out.println("所有学生信息:");
        for (Student student : students) {
            System.out.println(student);
        }
    }
}
public class Student001 {
    public static void main(String[] args) {
        StudentManagementSystem sms = new StudentManagementSystem();

        // 创建学生对象
        Student student1 = new Student("Alice", "S001");
        Student student2 = new Student("Bob", "S002");
        Student student3 = new Student("Alice", "S001"); // 同名且同学号的学生

        // 添加学生
        sms.addStudent(student1); // 添加成功
        sms.addStudent(student2); // 添加成功
        sms.addStudent(student3); // 不允许添加

        // 打印所有学生信息
        sms.printAllStudents();
    }

}
